import { promises as fs } from "node:fs";
import path from "node:path";
import { shuffle } from "./shuffle.js";

/**
 *
 * @param {string} input "input.txt"
 * @param {string} output "output.json"
 * @param {boolean} preserveBreak
 * @returns
 */
export async function poemCompile({ input, output, preserveBreak }) {
  const srcContent = await fs.readFile(path.resolve(input), "utf-8");

  // TODO Now only support 20 chars(5x4) 五言四句
  const size = 20; // srcContent.length;
  // console.log(size);

  const in_out_map = shuffle(size);

  const outputContent = [];
  for (let i = 0; i < size; i++) {
    outputContent.push(srcContent[in_out_map[i]]);
  }

  // TODO: duplicate letters

  const outputObj = {
    content: outputContent.join(""),
    mappings: in_out_map,
  };

  // write file
  await fs.writeFile(path.resolve(output), JSON.stringify(outputObj), "utf-8");
}
